//
//  MMGPurchasesAlert.m
//  DollsVending
//
//  Created by Don on 2017/10/15.
//  Copyright © 2017年 Memego, Inc. All rights reserved.
//

#import "MMGPurchasesAlert.h"
#import "UIImage+CacheViaName.h"
#import "MMGProduceCollectionViewCell.h"
#import "MMGProduceModel.h"

#import <SVProgressHUD.h>

@interface MMGPurchasesAlert ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
@property (nonatomic, strong) UIView *alertContainerView;
@property (nonatomic, strong) UIButton *closeButton;
/**
 显示选项的 collectionView
 */
@property (nonatomic, strong) UICollectionView *producesCollectionView;
@property (nonatomic, strong) NSMutableArray *producesArray;
@end
@implementation MMGPurchasesAlert

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        NSArray *produceIDs = @[@"com.memego.prizeclaw.ten",@"com.memego.prizeclaw.thirty"/*,@"com.memego.prizeclaw.ninety",@"com.memego.prizeclaw.188",@"com.memego.prizeclaw.388",@"com.memego.prizeclaw.588"*/];
        NSArray *produces = @[@"60",@"120"/*,@"692",@"1566",@"3230",@"5345"*/];
        NSArray *prices = @[@"￥6",@"￥12"/*,@"￥88",@"￥188",@"￥388",@"￥588"*/];
        for (int i = 0; i < 2; ++i) {
            MMGProduceModel *produce = [[MMGProduceModel alloc] init];
            produce.produceName = produces[i];
            produce.price = prices[i];
            produce.produceId = produceIDs[i];
            
            [self.producesArray addObject:produce];
        }
        
        [self setUpUI];
    }
    
    return self;
    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return  NO;
}
#pragma mark - UICollectionDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.producesArray.count;
}

- (MMGProduceCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MMGProduceCollectionViewCell";
    MMGProduceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    MMGProduceModel *produce = self.producesArray[indexPath.row];
    cell.produceLabel.text = produce.produceName;
    cell.priceLabel.text = produce.price;
    
    return cell;
}

#pragma mark - delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    DLog(@"collection view did selected ..");
    MMGProduceModel *produce = self.producesArray[indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)kInAppPurchaseTransactionReceiptNotification object:produce];
    
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


- (void)setUpUI {
    
   
    
    [self.alertContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(kCGAlertWidth, kCGAlertHeight));
    }];
    
    [self.producesCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.alertContainerView).offset(23.5);
        make.right.equalTo(self.alertContainerView).offset(-23.5);
        make.top.equalTo(self.alertContainerView.mas_top).offset(8);
        make.bottom.equalTo(self.alertContainerView.mas_bottom).offset(-8);
    }];
    
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.alertContainerView.mas_right).offset(-6);
        make.centerY.equalTo(self.alertContainerView.mas_top).offset(6);
        make.size.mas_equalTo(CGSizeMake(29, 29));
    }];
    
    
}
#pragma mark - lazy init
- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage mmg_imageNamed:@"closeUpdateAlertViewButton.png"] forState:UIControlStateNormal];
        [_closeButton setImage:[UIImage mmg_imageNamed:@"closeUpdateAlertViewButton.png"] forState:UIControlStateHighlighted];
        
        __weak typeof(self) weakSelf = self;
        [[_closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf dismissAlert];
        }];
        
        [self.alertContainerView addSubview:_closeButton];
    }
    
    return _closeButton;
}

- (UIView *)alertContainerView {
    if (!_alertContainerView) {
        _alertContainerView = [[UIView alloc] init];
        _alertContainerView.layer.cornerRadius = 10;
        _alertContainerView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:_alertContainerView];
    }
    
    return _alertContainerView;
}

- (UICollectionView *)producesCollectionView {
    if (!_producesCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumLineSpacing = 8.5;
        layout.minimumInteritemSpacing = 20;
        CGFloat width = (kCGAlertWidth - 80)*0.5;
        layout.itemSize = CGSizeMake(width, width);
        _producesCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _producesCollectionView.dataSource = self;
        _producesCollectionView.delegate = self;
        _producesCollectionView.showsVerticalScrollIndicator = NO;
        _producesCollectionView.showsHorizontalScrollIndicator = NO;
        _producesCollectionView.backgroundColor = [UIColor whiteColor];
        _producesCollectionView.pagingEnabled = NO;
        [_producesCollectionView registerClass:[MMGProduceCollectionViewCell class] forCellWithReuseIdentifier:@"MMGProduceCollectionViewCell"];
        
        [self addSubview:_producesCollectionView];
    }
    
    return _producesCollectionView;
}

- (NSMutableArray *)producesArray {
    
    if (!_producesArray) {
        _producesArray = [[NSMutableArray alloc] init];
    }
    return _producesArray;
    
}
@end
